package com.jwb.pub.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.service.CacheService;
import com.jwb.util.MainController;
@Slf4j
@Controller
@RequestMapping("/pub/cache")
public class CacheController  extends MainController {

	// 缓存服务
	@Autowired
	private RedisTemplate redisTemplate;
	
	@Resource
	private CacheService cacheService;

	
	@RequestMapping(value = "/cachelist")
	@ResponseBody
	public JSONObject cachelist(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsobject = new JSONObject();
		try{
			JSONArray result = new JSONArray();
			Set<String> keys = redisTemplate.keys("*");
			for(String key:keys){
				JSONObject jso = new JSONObject();
				String type = redisTemplate.type(key).name();
				jso.put("key", key);
				if(type.toLowerCase().equals("string")){
					Object obj = redisTemplate.opsForValue().get(key);
					System.out.println(obj.getClass().getName());
					jso.put("type", obj.getClass().getName());
					if(obj instanceof JSONObject){  
						jso.put("value", ((JSONObject)obj).toString());						
					}else if(obj instanceof String){
						jso.put("value", (String) obj);
					}else{
						jso.put("value", obj.getClass().getName());
					}
					
				}else{
					jso.put("value", type);
				}
				result.add(jso);
			}
			
			List<JSONObject> list = new ArrayList<JSONObject>();
			for(int i=0;i<result.size();i++){
				list.add(result.getJSONObject(i));
			}
			Collections.sort(list, new Comparator<JSONObject>(){
			    @Override
			    public int compare(JSONObject o1, JSONObject o2) {
			        String a = o1.getString("key");
			        String b = o2.getString("key");
			        return a.compareTo(b);
			    }
			});
			result.clear();
			for(int j=0;j<list.size();j++){
				result.add(list.get(j));
			}
			
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "ok");
			jsobject.put("list", result);
		}catch(Exception e){
			e.printStackTrace();
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "获取缓存失败");
		}
		return jsobject;
	}
	
	@RequestMapping(value = "/newcache")
	@ResponseBody
	public JSONObject newcache(HttpServletRequest req, HttpServletResponse res) {
		JSONObject jso = new JSONObject();
		String key = req.getParameter("key");
		String value = req.getParameter("value");
		try{
			if(redisTemplate.hasKey(key)){
				jso.put("respCode", "9");
				jso.put("respDesc", "当前键值已存在");
			}else{
				redisTemplate.opsForValue().set(key, value);
				jso.put("respCode", "0");
				jso.put("respDesc", "OK");
			}			
		}catch(Exception e){
			e.printStackTrace();
			jso.put("respCode", "-1");
			jso.put("respDesc","添加缓存失败");
		}
		
		return jso;		
	}

	@RequestMapping(value = "/delcache")
	@ResponseBody
	public JSONObject delcache(HttpServletRequest req, HttpServletResponse res){
		JSONObject jsobject = new JSONObject();
		String key = req.getParameter("key");
		try{
			redisTemplate.delete(key);
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
		}catch(Exception e){
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "删除缓存失败");
		}
		return jsobject;
	}
	
	@RequestMapping(value = "/clearcache")
	@ResponseBody
	public JSONObject clearcache(HttpServletRequest req, HttpServletResponse res){
		String key = req.getParameter("key");
		JSONObject jsobject = new JSONObject();
		try{
			String type = redisTemplate.type(key).name();
			if(type.toLowerCase().equals("string")){
				redisTemplate.opsForValue().set(key, "");
			}
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
		}catch(Exception e){
			e.printStackTrace();
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "清空缓存失败");
		}
		return jsobject;		
	}
	

}
